



//% color="#fe6900" iconWidth=50 iconHeight=40
namespace miniewaArduino{

   //% block="initialization wifi ids[id] pwsd[pwd]" blockType="command"
    //% id.shadow="string"   id.defl=gdwrobots
	//% pwd.shadow="string"  pwd.defl="05712345"
    export function initialization(parameter: any, block: any) {
        let wifi_ap = parameter.id.code;
        let wifi_pwd = parameter.pwd.code;
		Generator.addDefine(`_SS_MAX_RX_BUFF`,`512`); // RX buffer size
		Generator.addInclude(`softwareSerial`,`#include <SoftwareSerial.h>`,true);
		Generator.addObject("wifiMode","SoftwareSerial","wifimode(2,4)",true);
		//Generator.addObject("wifiState","String","wifistate",true);
		Generator.addCode(`wifimode.begin(9600);`);
		Generator.addCode(`wifimode.println(\"AT+CWMODE=1\\r\");\n \tdelay(1000);\n\twhile(wifimode.available()){\n\t\twifimode.read();\n\t}`);
		Generator.addCode(`wifimode.print("AT+CWJAP=\\"");\n\twifimode.print(${wifi_ap});\n\twifimode.print("\\",\\"");\n\twifimode.print(${wifi_pwd});\n\twifimode.print("\\"\\r\\n");\n\tdelay(2000);`);
        Generator.addCode(`while(wifimode.available()){\n\t\twifimode.read();\n\t}`);
    }

	//设置easyiot账户信息
	//% block="Set the mqtt server Easyiot iot_id[id] iot_pwd[pwd] " blockType="command"
    //% id.shadow="string"   id.defl=" "
	//% pwd.shadow="string"   pwd.defl=" "
	export function set_easyiot_id(parameter: any, block: any) {
        let iot_id = parameter.id.code;
        let iot_pwd = parameter.pwd.code;
		Generator.addCode(`wifimode.print("AT+MQTTUSERCFG=0,1,\\"iot.dfrobot.com.cn\\",\\"");`);
		Generator.addCode(`wifimode.print(${iot_id});`);
		Generator.addCode(`wifimode.print("\\",\\"");`);
		Generator.addCode(`wifimode.print(${iot_pwd});`);
		Generator.addCode(`wifimode.print("\\",0,0,\\"\\"\\r\\n");\n\tdelay(1000);`);
		Generator.addCode(`while(wifimode.available()){\n\t\twifimode.read();\n\t}`);
		Generator.addCode(`wifimode.print("AT+MQTTCONN=0,\\"iot.dfrobot.com.cn\\",1883,1\\r\\n");\n\tdelay(2000);`);     	 
		Generator.addCode(`while(wifimode.available()){\n\t\twifimode.read();\n\t}`);
	}


	//设置mqtt订阅列表"Topic_0~4"
	//% block="Subscribe to the topic list T0[Topic_0]  T1[Topic_1] T2[Topic_2] T3[Topic_3] T4[Topic_4] " blockType="command"
    //% Topic_0.shadow="string"   Topic_0.defl=" "
	//% Topic_1="string"   Topic_1.defl=" "
	//% Topic_2="string"   Topic_2.defl=" "
	//% Topic_3="string"   Topic_3.defl=" "
	//% Topic_4="string"   Topic_4.defl=" "
	export function Subscribe_to_the_topic_list(parameter: any, block: any) {
		let topic0 = parameter.Topic_0.code;
		let topic1 = parameter.Topic_1.code;
		let topic2 = parameter.Topic_2.code;
		let topic3 = parameter.Topic_3.code;
		let topic4 = parameter.Topic_4.code;
		Generator.addObject(`topic0`,`String`,`topic0=${topic0}`,true);
		Generator.addObject(`topic1`,`String`,`topic1=${topic1}`,true);
		Generator.addObject(`topic2`,`String`,`topic2=${topic2}`,true);
		Generator.addObject(`topic3`,`String`,`topic3=${topic3}`,true);
		Generator.addObject(`topic4`,`String`,`topic4=${topic4}`,true);
        Generator.addCode(`wifimode.print("AT+MQTTSUB=0,\\"");`);
		Generator.addCode(`wifimode.print(${topic0});`);
		Generator.addCode(`wifimode.print("\\",0\\r\\n");\n\tdelay(200);`);
		Generator.addCode(`while(wifimode.available()){\n\t\twifimode.read();\n\t}`);
		Generator.addCode(`wifimode.print("AT+MQTTSUB=0,\\"");`);
		Generator.addCode(`wifimode.print(${topic1});`);
		Generator.addCode(`wifimode.print("\\",0\\r\\n");\n\tdelay(200);`);
		Generator.addCode(`while(wifimode.available()){\n\t\twifimode.read();\n\t}`);
		Generator.addCode(`wifimode.print("AT+MQTTSUB=0,\\"");`);
		Generator.addCode(`wifimode.print(${topic2});`);
		Generator.addCode(`wifimode.print("\\",0\\r\\n");\n\tdelay(200);`);
		Generator.addCode(`while(wifimode.available()){\n\t\twifimode.read();\n\t}`);
		Generator.addCode(`wifimode.print("AT+MQTTSUB=0,\\"");`);
		Generator.addCode(`wifimode.print(${topic3});`);
		Generator.addCode(`wifimode.print("\\",0\\r\\n");\n\tdelay(200);`);
		Generator.addCode(`while(wifimode.available()){\n\t\twifimode.read();\n\t}`);
		Generator.addCode(`wifimode.print("AT+MQTTSUB=0,\\"");`);
		Generator.addCode(`wifimode.print(${topic4});`);
		Generator.addCode(`wifimode.print("\\",0\\r\\n");\n\tdelay(200);`);	
		Generator.addCode(`while(wifimode.available()){\n\t\twifimode.read();\n\t}`);
    }

	
	//向Topic发送信息
	//% block="Send message[msg] to Topic[topic]" blockType="command"
    //% topic.shadow="dropdown" topic.options="topics"  topic.defl="topics.topic1"
	//% msg.shadow = "string"  msg.defl=" "
	export function sendmessage(parameter: any, block: any) {
		let topicn = parameter.topic.code;
		let msgg = parameter.msg.code;
		Generator.addCode(`wifimode.print("AT+MQTTPUB=0,\\"");`);
		if(topicn ==="topic0"){
			Generator.addCode(`wifimode.print(topic0);`);
		}else if(topicn === "topic1"){
			Generator.addCode(`wifimode.print(topic1);`);
		}else if(topicn === "topic2"){
			Generator.addCode(`wifimode.print(topic2);`);
		}else if(topicn === "topic3"){
			Generator.addCode(`wifimode.print(topic3);`);
		}else if(topicn === "topic4"){
			Generator.addCode(`wifimode.print(topic4);`);
		}
		Generator.addCode(`wifimode.print("\\",\\"");`);
		Generator.addCode(`wifimode.print(${msgg});`);
		Generator.addCode(`wifimode.print("\\",0,0\\r\\n");`);
		Generator.addCode(`while(wifimode.available()){\n\t\twifimode.read();\n\t}`);
}
	
	
	//如果有topic信息
	//% block="Mqtt has information?" blockType="boolean"
	export function ifgetmsg(parameter: any, block: any) {
		Generator.addCode(`wifimode.available()`);
	}
	
	//读取topic信息
	//% block="get mqtt msg" blockType="reporter"
	export function getmsg(parameter: any, block: any) {
		Generator.addCode(`String(char(wifimode.read()))`);
	}
	
	//处理mqtt字符串信息
	//% block="[msgggg] is [type]" blockType="reporter"
	//% msgggg.shadow= "string"  
	//% type.shadow="dropdown" type.options="topic_type" type.defl="topic_type.topic"
   export function makemqttmsg(parameter: any, block: any) {
	   let msgg = parameter.msgggg.code;
	   let types = parameter.type.code;
	   //+MQTTSUBRECV:0,"C13ohpW4R",8,->zuozuo
	   //Generator.addInclude("dfString","#include <DFString.h>",true);
	   if(types === "topic"){
		   Generator.addCode(`${msgg}.substring((${msgg}.indexOf("\\""))+1,(${msgg}.lastIndexOf("\\"")))`);
			//Generator.addCode(`${msggk}`);	
	  }else if(types === "msg"){
		  Generator.addCode(`${msgg}.substring(((${msgg}.lastIndexOf(","))+1),(${msgg}.length()))`); 
	   }
	  // Generator.addCode
	   
   }
   
   //topic变量
   //% block="[topicnum]" blockType="reporter"
   //%topicnum.shadow="dropdown" topicnum.options="topicc" topicnum.defl="topicc.topic0"
   export function topiccs(parameter:any,block:any){
	   let topi = parameter.topicnum.code;
	   if(topi === "topic0"){
		   Generator.addCode(`topic0`);
	   }else if(topi === "topic1"){
		   Generator.addCode(`topic1`);
	   }else if(topi === "topic2"){
		   Generator.addCode(`topic2`);
	   }else if(topi === "topic3"){
		   Generator.addCode(`topic3`);
	   }else if(topi === "topic4"){
		   Generator.addCode(`topic4`);
	   }
	   
   }
   
   
   //软串口监听启动与停止
   //% block="[softnum] set listen [softstate]" blockType="command"
   //%softnum.shadow="dropdown" softnum.options="softNum" softnum.defl="softNum.wifi"
   //%softstate.shadow="dropdown" softstate.options="softState" softstate.defl="softState.softstart"
   export function softlisten(parameter:any,block:any){
	   let softn = parameter.softnum.code;
	   let softs = parameter.softstate.code;
	   if(softn === "wifi"){
		   if(softs === "softstart"){
			   Generator.addCode(`wifimode.listen();`);
		   }else if(softs === "softstop"){
			   Generator.addCode(`wifimode.end();`);
		   }
		   
		}else if(softn === "soft1"){
			if(softs === "softstart"){
			   Generator.addCode(`softwareSerial1.listen();`);
		   }else if(softs === "softstop"){
			   Generator.addCode(`softwareSerial1.end();`);
		   }
			
		}else if(softn === "soft2"){
			if(softs === "softstart"){
			   Generator.addCode(`softwareSerial2.listen();`);
		   }else if(softs === "softstop"){
			   Generator.addCode(`softwareSerial2.end();`);
		   }
			
		}
	   
   }
   
   
	
	
	
	/*Generator.addCode(`while(wifimode.available()){\n\t\twifistate += String(char(wifimode.read()));\n\t}`);
	Generator.addCode(`Serial.begin(9600);\n\tSerial.println(wifistate);`); */

	//初始化电机模块
 	//设置电机1~2正转反转与速度~qwqwqwqwqwqwqwqwqwqwqwqwqw123
	//% block="set motor num[Mnum] FX[MFx] Speed[SPEED]" blockType="command"
	//% Mnum.shadow="dropdown" Mnum.options="motorNum" Mnum.defl="motorNum.1"
    //% MFx.shadow="dropdown" MFx.options="motorFx" MFx.defl="motorFx.1"
   //% SPEED.shadow="range" SPEED.params.min="0" SPEED.params.max="255" SPEED.defl="200"
    export function setmotorsp(parameter: any, block: any) {
            let mnum = parameter.Mnum.code
            let mfx = parameter.MFx.code
            let sp = parameter.SPEED.code
            if(mnum === "motor2"){
                Generator.addSetup("set12up","pinMode(12,OUTPUT);",true);
                Generator.addSetup("set13up","pinMode(13,OUTPUT);",true);
                if(mfx === "fo"){
                    Generator.addCode(`digitalWrite(12,HIGH);`);
                    Generator.addCode(`digitalWrite(13,LOW);`);
                }else if(mfx === "re"){
                    Generator.addCode(`digitalWrite(12,LOW);`);
                    Generator.addCode(`digitalWrite(13,HIGH);`);
                }
                Generator.addCode(`analogWrite(11, ${sp});`);
            }else if(mnum === "motor1"){
                Generator.addSetup("set7up","pinMode(7,OUTPUT);",true);
                Generator.addSetup("set8up","pinMode(8,OUTPUT);",true);
                if(mfx === "fo"){
                    Generator.addCode(`digitalWrite(7,HIGH);`);
                    Generator.addCode(`digitalWrite(8,LOW);`);
                }else if(mfx === "re"){
                    Generator.addCode(`digitalWrite(7,LOW);`);
                    Generator.addCode(`digitalWrite(8,HIGH);`);
                }
                Generator.addCode(`analogWrite(6, ${sp});`);
            }

    }

    //关停电机
 	//设置电机1~2正转反转与速度~qwqwqwqwqwqwqwqwqwqwqwqwqw123
	//% block="close [mnums] " blockType="command"
	//%mnums.shadow="dropdown" mnums.options="closemotornum" mnums.defl="closemotornum.all"
    export function stopMotor(parameter: any, block: any) {
            let mnum = parameter.mnums.code
            if(mnum === "motor2"){
                Generator.addCode(`digitalWrite(12,LOW);`);
                Generator.addCode(`digitalWrite(13,LOW);`);
                Generator.addCode(`analogWrite(11, 0);`);
            }else if(mnum === "motor1"){
                Generator.addCode(`digitalWrite(7,LOW);`);
                Generator.addCode(`digitalWrite(8,LOW);`);
                Generator.addCode(`analogWrite(6, 0);`);
            }else if(mnum === "all"){
                Generator.addCode(`digitalWrite(7,LOW);`);
                Generator.addCode(`digitalWrite(8,LOW);`);
                Generator.addCode(`analogWrite(6, 0);`);
                Generator.addCode(`digitalWrite(12,LOW);`);
                Generator.addCode(`digitalWrite(13,LOW);`);
                Generator.addCode(`analogWrite(11, 0);`);
            }

    }
	
	//初始化gps/北斗模块硬串口
	//%block="Init gnss hardSerial RX(yellow)[rxpin] TX(white)[txpin]" blockType = "command"
	//%rxpin.shadow="dropdown" rxpin.options="hardserialrx" rxpin.defl="hardserialrx.0"
	//%txpin.shadow="dropdown" txpin.options="hardserialtx" txpin.defl="hardserialtx.1"
	export function gnss_hardSerial(parameter: any, block: any){
		Generator.addInclude("tinygpsh","#include <TinyGPSPlus.h>",true);
		Generator.addObject("gnssobject","TinyGPSPlus","gnss",true);
		Generator.addSetup("gnsshardSerial","Serial.begin(9600);",true);
		
	}
	
	//初始化gps/北斗模块软串口
	//% block="Init gnss softwareSerial RX(yellow)[rxpin1] TX(white)[txpin1]" blockType = "command"
	//%rxpin1.shadow="dropdown" rxpin1.options="gnss_softwareserialrx" rxpin1.defl="gnss_softwareserialrx.er"
	//%txpin1.shadow="dropdown" txpin1.options="gnss_softwareserialtx" txpin1.defl="gnss_softwareserialtx.wu"
	export function gnss_softwareSerial(parameter: any, block: any){
		let rx = parameter.rxpin1.code;
		let tx = parameter.txpin1.code;
		Generator.addInclude("tinygpsh","#include <TinyGPSPlus.h>",true);
		Generator.addInclude("softwareserial","#include <SoftwareSerial.h>",true);
		Generator.addObject("gnssobject","TinyGPSPlus","gnss",true);
		if(rx === "er" && tx === "wu"){
			Generator.addObject("gnsssoftSerial","SoftwareSerial","gnssSerial(2,5)",true);
			Generator.addSetup("gnsssoftwraeSerial","gnssSerial.begin(9600);",true);
		}else if(rx === "san" && tx === "si"){
			Generator.addObject("gnsssoftSerial","SoftwareSerial","gnssSerial(3,4)",true);
			Generator.addSetup("gnsssoftwraeSerial","gnssSerial.begin(9600);",true);
		}	
	}

	//如果gnss获得数据
	//% block="if serial[serial_type]gnss get data" blockType="boolean"
	//%serial_type.shadow="dropdown" serial_type.options="gnss_serial_type" serial_type.defl="gnss_serial_type.soft"
	export function If_gnss_getData(parameter: any, block: any){
		let sertype = parameter.serial_type.code;
		if(sertype === "soft"){
		Generator.addCode(`gnssSerial.available()`);
		}else if(sertype === "hard"){
		Generator.addCode(`Serial.available()`);
		}
	}

	//如果gnss解析数据成功
	//% block="if [serial_type]gnssData Init get?" blockType="boolean"
	//%serial_type.shadow="dropdown" serial_type.options="gnss_serial_type" serial_type.defl="gnss_serial_type.soft"
	export function If_gnss_Initsucc(parameter: any, block: any){
		let sertype = parameter.serial_type.code;
		if(sertype === "soft"){
		Generator.addCode(`gnss.encode(gnssSerial.read())`);
		}else if(sertype === "hard"){
		Generator.addCode(`gnss.encode(Serial.read())`);
		}
	}
	
	//如果定位数据中位置/日期/时间有效时
	//% block="If [Data_type] is valid?" blockType="boolean"
	//%Data_type.shadow="dropdown" Data_type.options="flag_gnssData" Data_type.defl="flag_gnssData.wz"
	export function If_gnss_valid(parameter: any, block: any){
		let datatype = parameter.Data_type.code;
		if(datatype === "wz"){
		Generator.addCode(`gnss.location.isValid()`);
		}else if(datatype === "rq"){
		Generator.addCode(`gnss.date.isValid()`);	
		}else if(datatype === "sj"){
		Generator.addCode(`gnss.time.isValid()`);
			
		}
	}
	
	//如果定位数据中位置/日期/时间有效时
	//% block="get gnssData [info]" blockType="reporter"
	//%info.shadow="dropdown" info.options="gnss_info" info.defl="gnss_info.jd"
	export function get_gnss_info(parameter: any, block: any){
		let inf = parameter.info.code;
		if(inf === "jd"){
		Generator.addCode(`gnss.location.lng()`);
		}else if(inf === "wd"){
		Generator.addCode(`gnss.location.lat()`);	
		}else if(inf === "year"){
		Generator.addCode(`gnss.date.year()`);	
		}else if(inf === "month"){
		Generator.addCode(`gnss.date.month()`);	
		}else if(inf === "date"){
		Generator.addCode(`gnss.date.day()`);	
		}else if(inf === "hour"){
		Generator.addCode(`((gnss.time.hour())+8)%24`);	
		}else if(inf === "min"){
		Generator.addCode(`gnss.time.minute()`);	
		}else if(inf === "sec"){
		Generator.addCode(`gnss.time.second()`);	
		}
	}
	
	
	
	
	
}
